package com.guqin.interview.controller;

import com.guqin.interview.common.BaseResponse;
import com.guqin.interview.common.ErrorCode;
import com.guqin.interview.common.ResultUtils;
import com.guqin.interview.exception.BusinessException;
import com.guqin.interview.model.entity.Question;
import com.guqin.interview.model.entity.User;
import com.guqin.interview.service.QuestionFavoriteService;
import com.guqin.interview.service.QuestionService;
import com.guqin.interview.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 题目收藏接口
 */
@RestController
@RequestMapping("/question/favorite")
public class QuestionFavoriteController {

    @Resource
    private QuestionFavoriteService questionFavoriteService;

    @Resource
    private QuestionService questionService;

    @Resource
    private UserService userService;

    /**
     * 收藏题目
     *
     * @param questionId 题目 id
     * @param request 请求
     * @return 是否成功
     */
    @PostMapping("/add")
    public BaseResponse<Boolean> favoriteQuestion(@RequestParam Long questionId, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        boolean result = questionFavoriteService.favoriteQuestion(loginUser.getId(), questionId);
        return ResultUtils.success(result);
    }

    /**
     * 取消收藏题目
     *
     * @param questionId 题目 id
     * @param request 请求
     * @return 是否成功
     */
    @PostMapping("/remove")
    public BaseResponse<Boolean> unfavoriteQuestion(@RequestParam Long questionId, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        boolean result = questionFavoriteService.unfavoriteQuestion(loginUser.getId(), questionId);
        return ResultUtils.success(result);
    }

    /**
     * 获取用户收藏的题目列表
     *
     * @param request 请求
     * @return 题目列表
     */
    @GetMapping("/list")
    public BaseResponse<List<Question>> getUserFavoriteQuestions(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        List<Long> questionIds = questionFavoriteService.getUserFavoriteQuestionIds(loginUser.getId());
        if (questionIds.isEmpty()) {
            return ResultUtils.success(List.of());
        }
        List<Question> questions = questionService.listByIds(questionIds);
        return ResultUtils.success(questions);
    }

    /**
     * 判断用户是否收藏了某道题目
     *
     * @param questionId 题目 id
     * @param request 请求
     * @return 是否收藏
     */
    @GetMapping("/check")
    public BaseResponse<Boolean> isQuestionFavorited(@RequestParam Long questionId, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        boolean result = questionFavoriteService.isQuestionFavorited(loginUser.getId(), questionId);
        return ResultUtils.success(result);
    }
} 